<?php

class Helper_Counter
{
	/*统计资料,1当天2历史3将来4跨越*/
	
	public function AllCount($starttime,$endtime){
		if($starttime==$endtime){
					$endd=$this->my_date_add('d',$endtime,0);

					if($starttime==date("Y-m-d")){
						//$istoday=true;
						return '1';
					}else{
						if($this->DateDiff(date("Y-m-d"),$starttime)<0){
							//$ishistory=true;
							return '2';
						}else{
							//$isfuntrue=true;
							return '3';
						}
					}
					//$iscross=false;

		}else{
			$endd=$endtime;
					
			$allsti=$this->DateDiff(date("Y-m-d"),$starttime);
			$alleti=$this->DateDiff(date("Y-m-d"),$endtime);
					
			//dump($allsti*$alleti);
			if($allsti*$alleti<0){
				//$iscross=true;
				return '4';
			}else{
				if($allsti>0){
					//$isfuntrue=true;
					return '3';
				}else{
					//$ishistory=true;
					return '2';
				}
			}
		}
	}

	/*按日期统计行为日志*/
	public function Counter_config($type,$addconds){
		$table =QDB::getConn();

		//列出所有主机的MAC地址
		$sql = "SELECT DISTINCT macaddress FROM `hosttable`";
		$handel =$table->execute($sql);
		$temp2 = $handel->fetchAll();
		$allhave2 = array();
		foreach ($temp2 as $s){
			$allhave2[$s['macaddress']]=array();
			//echo $s['groupid'];
		}
		
		/*统计资料,1当天2历史3将来4跨越*/
		//默认列出今天的所有主机的行为日志的统计资料(总)
		if($type=='1' || $type=='4'){
			$addconds_0=str_replace('time','date',$addconds);
			$sql = "SELECT count( 1 ) AS macactions, `macaddress`,sum( `security` ) AS securitys FROM `action_all` WHERE ".$addconds_0." GROUP BY `macaddress`";

			$handel =$table->execute($sql);
			$temp3 = $handel->fetchAll();
			foreach ($temp3 as $s){
				$allhave2[$s['macaddress']]['actcounts']=$s['macactions'];
				$allhave2[$s['macaddress']]['actalarms']=$s['securitys'];
				$allhave2[$s['macaddress']]['actsecuritys']=$allhave2[$s['macaddress']]['actcounts']-$allhave2[$s['macaddress']]['actalarms'];
			}

			$sql = "SELECT count( 1 ) AS macactions, `macaddress`,sum( `security` ) AS securitys FROM `eventlog_all` WHERE ".$addconds." GROUP BY `macaddress`";

			$handel =$table->execute($sql);
			$temp3 = $handel->fetchAll();
			foreach ($temp3 as $s){
				$allhave2[$s['macaddress']]['evtcounts']=$s['macactions'];
				$allhave2[$s['macaddress']]['evtalarms']=$s['securitys'];
				$allhave2[$s['macaddress']]['evtsecuritys']=$allhave2[$s['macaddress']]['evtcounts']-$allhave2[$s['macaddress']]['evtalarms'];
			}
		}else if($type=='2' || $type=='4'){
			$sql = "SELECT sum( actiontotal ) AS ats, sum( actionalarm ) AS als, sum( eventtotal ) AS ets, sum( eventalarm ) AS els, `macaddress` FROM `statisticstable` WHERE ".str_replace('time','date',$addconds)." GROUP BY `macaddress`";
			//dump($sql);
			
			$handel =$table->execute($sql);
			$temp5 = $handel->fetchAll();
			foreach ($temp5 as $s){
			
				if(isset($allhave2[$s['macaddress']]['actcounts']))
					$allhave2[$s['macaddress']]['actcounts']=$allhave2[$s['macaddress']]['actcounts']+$s['ats'];
				else
					$allhave2[$s['macaddress']]['actcounts']=$s['ats'];
				
				if(isset($allhave2[$s['macaddress']]['actalarms']))
					$allhave2[$s['macaddress']]['actalarms']=$allhave2[$s['macaddress']]['actalarms']+$s['als'];
				else
					$allhave2[$s['macaddress']]['actalarms']=$s['als'];
					
				//dump($s['ats']);
				if(isset($allhave2[$s['macaddress']]['actsecuritys']))
					$allhave2[$s['macaddress']]['actsecuritys']=$allhave2[$s['macaddress']]['actsecuritys']+($s['ats']-$s['als']);
				else
					$allhave2[$s['macaddress']]['actsecuritys']=($s['ats']-$s['als']);
					//echo $s['groupid'];
				//dump($s['ats']-$s['als']);
				if(isset($allhave2[$s['macaddress']]['evttcounts']))
					$allhave2[$s['macaddress']]['evtcounts']=$allhave2[$s['macaddress']]['evtcounts']+$s['ets'];
				else
					$allhave2[$s['macaddress']]['evtcounts']=$s['ets'];

				if(isset($allhave2[$s['macaddress']]['evtcounts']))
					$allhave2[$s['macaddress']]['evtsecuritys']=$allhave2[$s['macaddress']]['evtcounts']+($s['ets']-$s['els']);
				else
					$allhave2[$s['macaddress']]['evtsecuritys']=($s['ets']-$s['els']);
				
			}
		}

		return $allhave2;
	
	}

    public function my_date_add($opt,$time,$num){
		$year = date('Y',strtotime($time));
		$month = date('m',strtotime($time));
		$day = date('d',strtotime($time));
		$last_day = date('t',strtotime($time));
		if($opt=='y'){
			$year = $year + $num;
		}else if($opt=='d'){
			$day = $day + $num;
		}else if($opt=='m'){
			$month = $month + $num;
			if($day == $last_day){//如果是最后一天的话
				$day = date('t',strtotime("{$year}-{$month}-01"));
			}
		} else {
			return $time;
		}
		return date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
	}

	public function DateDiff($d1,$d2)
	{
		//日期比较函数
		 if(is_string($d1))$d1=strtotime($d1);
		 if(is_string($d2))$d2=strtotime($d2);
		 return ($d2-$d1)/86400; //这里返回的是天数，如果除以3600就是返回小时了，依此类推
	}
}
?>